# coding:utf8
from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler
from pyftpdlib.servers import FTPServer
import ConfigParser

class FtpServer(object):

    def __init__(self, config_path):
        self.config_path=config_path
    
    def start_server(self):
        '''
        启动ftp服务
        '''
        # 加载配置文件
        conf = ConfigParser.ConfigParser()
        authorizer = DummyAuthorizer()
        with open(self.config_path, 'r') as f:
            conf.readfp(f)
            users = conf.items("users")
            for user in users:
                user_path = conf.get("path", user[0])
                if user_path:
                    authorizer.add_user(user[0], user[1], user_path)
        #    authorizer.add_anonymous(conf.get("path", "anonymous"))

        # 设置用户权限
        handler = FTPHandler
        handler.authorizer = authorizer
        server = FTPServer(('0.0.0.0', 19221), handler)
        server.serve_forever()

if __name__ == "__main__":
    config_path = "config.ini"
    ftpserver = FtpServer(config_path)
    ftpserver.start_server()
